package com.springboot.springbootlogindemo.controller;

import com.springboot.springbootlogindemo.domain.EncryptText;
import com.springboot.springbootlogindemo.service.DesService;
import com.springboot.springbootlogindemo.service.UserService;
import com.springboot.springbootlogindemo.utils.DesUtils;
import com.springboot.springbootlogindemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * [说明/描述]
 *
 * @author 张怡冉
 * @version 版本号
 * @date {2024/4/26}
 */
@RestController
@RequestMapping("/des")
public class DesController {
    @Autowired
    private DesService desService;

    @PostMapping("/encrypt")
    public Result<String> encryptController(@RequestBody EncryptText encryptText) throws Exception {
        String encResult = desService.encrypt(encryptText.getText(), DesUtils.ORIGIN_KEY);

        if (encResult != null && !encResult.equals("")) {
            return Result.success(encResult, "加密成功");
        } else {
            return Result.error("400", "加密失败");
        }
    }

    @PostMapping("/decrypt")
    public Result<String> decryptController(@RequestBody EncryptText encryptText) throws Exception {
        String encResult = desService.decrypt(encryptText.getText(), DesUtils.ORIGIN_KEY);

        if (encResult != null && !encResult.equals("")) {
            return Result.success(encResult, "解密成功");
        } else {
            return Result.error("400", "解密失败");
        }
    }
}
